import json
from datetime import datetime

from django.core.paginator import Paginator
from django.http import JsonResponse

from lapi_app.models.testcase_model.testcase import Variable_local
from lapi_app.view.user_view.token_check import access_token


def del_var(request):
    try:
        data = json.loads(request.body)
        if not access_token(data["user_id"]):
            return JsonResponse({
                "code": 1001,
                "message": "登录失效，请重新登录"
            })
        if data["var_id"] == "":
            return JsonResponse({
                "code": 100,
                "message": "变量不可为空",
            })
        Variable_local.objects.filter(id=data["var_id"]).delete()
        return JsonResponse({
            "code": 200,
            "message": "删除成功"
        })
    except Exception as e:
        return JsonResponse({
            "code": 100,
            "message": str(e),
        })


def get_variable(request):
    data = json.loads(request.body)
    if not access_token(data["user_id"]):
        return JsonResponse({
            "code": 1001,
            "message": "登录失效，请重新登录"
        })
    var_result = []
    if data["var_id"] == "":
        return JsonResponse({
            "code": 100,
            "message": "变量不可为空",
        })
    elif Variable_local.objects.filter(id=data["var_id"]).count() == 0:
        return JsonResponse({
            "code": 100,
            "message": "env_id不存在",
        })
    else:
        vars = Variable_local.objects.filter(id=data["var_id"])
        for var in vars:
            var_dict = {
                "local_variable_name": var.local_name,
                "local_variable_result": var.local_result,
                "local_variable_description": var.local_description
            }
            var_result.append(var_dict)
        return JsonResponse({
            "code": 200,
            "message": "获取变量信息成功",
            "content": var_result
        })


# 获取接口信息中的变量列表
def get_var_list(request):
    data = json.loads(request.body)
    if not access_token(data["user_id"]):
        return JsonResponse({
            "code": 1001,
            "message": "登录失效，请重新登录"
        })
    if data["local_variable_name"] == "":
        variable = Variable_local.objects.all().order_by("-id")
    else:
        variable = Variable_local.objects.filter(local_name__contains=data["local_variable_name"])
    size_page = data["size_page"]
    current_pages = data["current_page"]
    case_total = len(variable)
    var_list = []
    for var in variable:
        var_dict = {
            "local_variable_id": var.id,
            "local_variable_name": var.local_name,
            "local_variable_data": var.local_data,
            "local_variable_result": var.local_result,
            "local_variable_description": var.local_description,
            "username": var.local_user.username
        }
        var_list.append(var_dict)
    p = Paginator(var_list, size_page)
    page1 = p.page(current_pages)
    current_page = page1.object_list
    return JsonResponse({
        "code": 200,
        "message": "获取变量列表成功",
        "content": current_page,
        "var_total": case_total,
        "size_page": size_page,
        "current_page": current_pages
    })
